﻿#region Copyright

// The contents of this file are subject to the Mozilla Public License
//  Version 1.1 (the "License"); you may not use this file except in compliance
//  with the License. You may obtain a copy of the License at
//  
//  http://www.mozilla.org/MPL/
//  
//  Software distributed under the License is distributed on an "AS IS"
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//  License for the specific language governing rights and limitations under 
//  the License.
//  
//  The Initial Developer of the Original Code is Robert Smyth.
//  Portions created by Robert Smyth are Copyright (C) 2008.
//  
//  All Rights Reserved.

#endregion

using System;
using System.Windows.Media;
using NUnitGrid.Framework.Logging;
using NUnitGrid.Framework.Plugins;
using NUnitGrid.Framework.UI;
using NUnitGrid.ProxyServer;


namespace NUnitGrid.GridClient.Plugins.GridClient.UI.GridLoading
{
    public class MyTasksProgressElementController : IUIChildPlugin, ITextBoxElementListener, IClientGridMetricsListener,
                                                    IRunSessionListener
    {
        private readonly Brush allPassedColour = new SolidColorBrush(Colors.Green);
        private readonly Brush failureColour = new SolidColorBrush(Colors.Red);
        private readonly ILogger logger;
        private readonly IProgressBarElement progressBar;
        private readonly Brush stoppingWithAllPassedColour = new SolidColorBrush(Colors.GreenYellow);
        private double totalSessionTaskCount;

        public MyTasksProgressElementController(IProgressBarElement progressBar, ILogger logger)
        {
            this.progressBar = progressBar;
            this.logger = logger;
        }

        public void Start(IHostView hostView)
        {
            progressBar.SetLabelText("My tasks progress:");
            progressBar.SetProgress(0.0);
            progressBar.Foreground = allPassedColour;
            hostView.AddChild(progressBar);
        }

        public void Stop(IHostView view)
        {
        }

        void IClientGridMetricsListener.OnUpdate(int pendingTasks, int runningTasks)
        {
            double currentTaskCount = pendingTasks + runningTasks;

            if (currentTaskCount == 0.0)
            {
                progressBar.ClearOuterGlow();
                if (totalSessionTaskCount > 0.0)
                {
                    progressBar.SetProgress(100.0);
                }
                totalSessionTaskCount = 0.0;
            }
            else
            {
                progressBar.ShowOuterGlow();
                var totalTasks = Math.Max(currentTaskCount, totalSessionTaskCount);
                totalSessionTaskCount = (int) totalTasks;
                progressBar.SetProgress((totalTasks - currentTaskCount)/totalTasks*100.0);
            }
        }

        void IRunSessionListener.OnStarted()
        {
            progressBar.Foreground = allPassedColour;
        }

        void IRunSessionListener.OnStopping()
        {
            progressBar.Foreground = stoppingWithAllPassedColour;
        }

        void IRunSessionListener.OnMesssage(string format, params object[] args)
        {
        }

        void IRunSessionListener.OnTaskCompleted(string fixture, bool succeeded, string nodeMachineName,
                                                 string textOutput)
        {
            if (!succeeded)
            {
                progressBar.Foreground = failureColour;
            }
        }

        void IRunSessionListener.OnCompleted(int passedCount, int failedCount, bool taskError)
        {
            if (failedCount > 0)
            {
                progressBar.Foreground = failureColour;
            }
        }

        void ITextBoxElementListener.OnTextChanged(string text)
        {
            ClientSettings.Default.TestAssembly = text;
            ClientSettings.Default.Save();
        }
    }
}